package com.elitel.frame.web.advice;

import com.elitel.common.core.domain.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * 全局异常统一处理
 *
 * @author mulis
 */
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionAdvice {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionAdvice.class);

    @ExceptionHandler(Exception.class)
    public JsonResult exceptionHandler(Exception e) {
        logger.error("系统异常", e);
        return JsonResult.error(e.getMessage());
    }


    @ExceptionHandler(SQLException.class)
    public JsonResult sqlExceptionHandler(Exception e) {
        logger.error("SQL异常", e);
        return JsonResult.error("数据库异常，请联系管理员");
    }


}
